package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.OperationLog;
import com.mall.db.domain.Brand;
import com.mall.db.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/bc")
public class BrandController extends BaseController<Brand> {

    @Resource(name = "brandService")
    private BrandService bs;


    @RequestMapping("/getFind")
    public Map<String,Object> getFind(Brand brand){
        PageHelper.startPage(brand.getPage(),brand.getLimit());
        QueryWrapper qw=new QueryWrapper();
        List<Brand> list= bs.list(qw);
        PageInfo<Brand> page=new PageInfo<>(list,brand.getLimit());
        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    @OperationLog(value = "品牌管理查询",type = 0)
    public Map<String, Object> find( Brand brand) {
        PageHelper.startPage(brand.getPage(),brand.getLimit());
        QueryWrapper qw=new QueryWrapper();
        if(brand.getBrandName()!=null){
        qw.eq("brand_name",brand.getBrandName());
        }
        List<Brand> list= bs.list(qw);
        PageInfo<Brand> page=new PageInfo<>(list,brand.getLimit());
        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> update(Brand brand) {
        boolean b=bs.updateById(brand);
        return this.iud("编辑",b,brand);
    }

    @Override
    public Map<String, Object> delete(Brand brand) {
        boolean b=bs.removeById(brand.getId());
        return this.iud("删除",b,brand);
    }


    @Override
    public Map<String, Object> add(Brand brand) {
        Map<String,Object> map=new HashMap<>();
        QueryWrapper qw=new QueryWrapper();
        qw.eq("brand_name",brand.getBrandName());
        List<Brand> list= bs.list(qw);
        if(list.size()==0){
            boolean b= bs.save(brand);
            return this.iud("新增",b,brand);
        }
        map.put("msg","新增失败,已存在名字为"+brand.getBrandName()+"的品牌");
        return map;
    }
}
